﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using ALMKickstarter.Data;
using ALMKickstarter.Models;

namespace ALMKickstarter.Infrastructure
{
	public class ProjectRepository
	{
		ALMKickstarterEntities context;

		public ProjectRepository()
		{
			this.context = new ALMKickstarterEntities();
		}

		public List<ProjectModel> GetProjects()
		{
			List<ProjectModel> projects = new List<ProjectModel>();
			foreach(var p in context.Projects)
				projects.Add(Mapper.Map<ProjectModel>(p));
			return projects;
		}

		public ProjectModel GetProjectById(int id)
		{
			return Mapper.Map<ProjectModel>(
				context.Projects.Single(s => s.ProjectId == id));
		}

		public void InsertProject(ProjectModel project)
		{
			context.Projects.Add(new Project(project.Name, project.Description));
			context.SaveChanges();
		}

		public void UpdateProject(ProjectModel project)
		{
			Project p = context.Projects.Single(s => s.ProjectId == project.ProjectId);
			p.Name = project.Name;
			p.Description = project.Description;
			context.SaveChanges();
		}

		public void DeleteProject(int id)
		{
			context.Projects.Remove(context.Projects.Single(s => s.ProjectId == id));
			context.SaveChanges();
		}
	}
}